package com.sscl.baselibrary.exts

import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
import java.io.InputStreamReader


inline fun <reified T> Any.saveAs(): T {
    return this as T
}

@Suppress("UNCHECKED_CAST")
fun <T> Any.saveAsUnChecked(): T {
    return this as T
}

inline fun <reified T> Any.isEqualType(): Boolean {
    return this is T
}

fun Any.getCpuInfo(): String? {
    try {
        val fr = FileReader("/proc/cpuinfo")
        val br = BufferedReader(fr)
        var line: String?
        val sb = StringBuilder()
        while (true) {
            line = br.readLine()
            if (line == null) break
            sb.append(line)
            sb.append("\n")
        }
        br.close()
        return sb.toString()
    } catch (e: IOException) {
        e.printStackTrace()
        return null
    }
}

fun Any.getCpuInfoByCmd(): String {
    val runtime = Runtime.getRuntime()
    val process = runtime.exec("cat /proc/cpuinfo")
    val br = BufferedReader(InputStreamReader(process.inputStream))
    var line: String?
    val sb = StringBuilder()
    while (true) {
        line = br.readLine()
        if (line == null) break
        sb.append(line)
        sb.append("\n")
    }
    return sb.toString()
}

